//
//  LYQEditPositionDefaultCell.m
//  ZCProject
//
//  Created by Zheng on 2016/12/25.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQEditPositionDefaultCell.h"

#import "LYQEditPositionModel.h"

@implementation LYQEditPositionDefaultCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if ([reuseIdentifier isEqualToString:kLYQEditPositionDefaultCell_ID3]) {
            self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        [self addSubviewsInContentViewWithReuseIdentifier:reuseIdentifier];
        
        [self layoutSubviewsInContentViewWithReuseIdentifier:reuseIdentifier];
        
    }
    return self;
}

#pragma mark - model
- (void)setModel:(LYQEditPositionModel *)model {
    _model = model;
    
    _cmNameTf.text = _model.commanyName;
 
}

#pragma mark - 编辑事件
- (void)valueChangedAction:(UITextField *)textField {

    
    if (textField == _cmNameTf) {
        _model.commanyName = textField.text;
        return;
    }
    
    if (textField == _pNameLTf) {
        
        if ([textField.text length] > 20) {
            textField.text = [textField.text substringToIndex:20];
        }
        
        _model.jobName = textField.text;
        return;
    }
}

#pragma mark - add subviews 
- (void)addSubviewsInContentViewWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    [self.contentView addSubview:self.titleLbl];
    
    if ([reuseIdentifier isEqualToString:kLYQEditPositionDefaultCell_ID1]) {
        [self.contentView addSubview:self.cmNameTf];
    } else if ([reuseIdentifier isEqualToString:kLYQEditPositionDefaultCell_ID2]) {
        [self.contentView addSubview:self.pNameLTf];
    } else {
        [self.contentView addSubview:self.valueLbl];
    }
}

#pragma mark - layout subviews
- (void)layoutSubviewsInContentViewWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kWScale(10));
        make.top.mas_equalTo(kWScale(5));
        make.bottom.mas_equalTo(-kWScale(5));
        make.height.equalTo(@(kWScale(40)));
        make.width.equalTo(@(kWScale(80)));
    }];
    
    if ([reuseIdentifier isEqualToString:kLYQEditPositionDefaultCell_ID1]) {
        
        [_cmNameTf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLbl.mas_right);
            make.top.bottom.height.equalTo(_titleLbl);
            make.right.mas_equalTo(-kWScale(15));
        }];
        
    } else if ([reuseIdentifier isEqualToString:kLYQEditPositionDefaultCell_ID2]) {
        
        [_pNameLTf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLbl.mas_right);
            make.top.bottom.height.equalTo(_titleLbl);
            make.right.mas_equalTo(-kWScale(15));
        }];

    } else {
     
        [_valueLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleLbl.mas_right);
            make.top.bottom.height.equalTo(_titleLbl);
            make.right.equalTo(self.contentView);
        }];
    }
}

#pragma mark - lazy load subviews
- (UILabel *)titleLbl {
    if (_titleLbl) {
        return _titleLbl;
    }
    
    _titleLbl = [[UILabel alloc] init];
    _titleLbl.font = kWFont(15);
    _titleLbl.textColor = kLYQBlack;
    
    return _titleLbl;
}

- (UITextField *)cmNameTf {
    if (_cmNameTf) {
        return _cmNameTf;
    }
    
    _cmNameTf = [[UITextField alloc] init];
//    _cmNameTf.text = @"博信(北京)会展有限公司";
    _cmNameTf.placeholder = @"输入公司名称(不超过20字)";
    _cmNameTf.font = kWFont(15);
    _cmNameTf.textAlignment = NSTextAlignmentLeft;
    _cmNameTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_cmNameTf addTarget:self action:@selector(valueChangedAction:) forControlEvents:UIControlEventEditingChanged];
    _cmNameTf.inputAccessoryView = self.inputAView;
    
    return _cmNameTf;
}

- (UITextField *)pNameLTf {
    if (_pNameLTf) {
        return _pNameLTf;
    }
    
    _pNameLTf = [[UITextField alloc] init];
    _pNameLTf.placeholder = @"输入职务2-10字";
    _pNameLTf.font = kWFont(15);
    _pNameLTf.textAlignment = NSTextAlignmentLeft;
    _pNameLTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_pNameLTf addTarget:self action:@selector(valueChangedAction:) forControlEvents:UIControlEventEditingChanged];
    _pNameLTf.inputAccessoryView = self.inputAView;

    return _pNameLTf;
}

- (UILabel *)valueLbl {
    if (_valueLbl) {
        return _valueLbl;
    }
    
    _valueLbl = [[UILabel alloc] init];
    _valueLbl.font = kWFont(15);
//    _valueLbl.text = @"卡六大数据库";
    
    return _valueLbl;
}

- (UIView *)inputAView {
    if (_inputAView) {
        return _inputAView;
    }
    
    _inputAView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMS_Width, kWScale(30))];
    _inputAView.backgroundColor = [UIColor whiteColor];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kMS_Width, 0.5)];
    lineView.backgroundColor = kLYQSystemGray;
    [_inputAView addSubview:lineView];
    
    _fBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _fBtn.frame = CGRectMake(kMS_Width-kWScale(65), 0.5, kWScale(60), kWScale(30)-0.5);
    [_fBtn setTitle:@"完成" forState:UIControlStateNormal];
    [_fBtn setTitleColor:kLYQBlue forState:UIControlStateNormal];
    _fBtn.titleLabel.font = kWFont(16);
    [_fBtn addTarget:self action:@selector(fBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_inputAView addSubview:_fBtn];
    
    return _inputAView;
}

#pragma mark - button 点击事件
- (void)fBtnAction {
    
    [self endEditing:YES];
    
}


@end
